import './App.css';
import React, { Component } from "react";
import { BrowserRouter as Router, Switch, Route,Redirect } from "react-router-dom";
import {getCookie} from './config/omsCookiesFun';
import Left from "./pages/components/common/left";
import Header from "./pages/components/common/header";
import Footer from "./pages/components/common/footer";
import Routers from "./router/routerMap";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      langKey:window.localStorage.getItem('lang') || 'cn',
      isLogin:getCookie('userInfo')?true:false
    }
    this.updateLangKey= this.updateLangKey.bind(this);
    this.updateIsLogin= this.updateIsLogin.bind(this);
  }
  updateLangKey(newVal){
    this.setState({langKey:newVal});
  }
  updateIsLogin(newVal){
    this.setState({isLogin:newVal});
  }
  assignProps(item){
    return Object.assign({}, {langKey:this.state.langKey,updateIsLogin: this.updateIsLogin}, item)
  }
  render() {
    var _this = this;
    return (
      <React.Fragment>
        <Router>
          <React.Fragment>
            <Header updateLangKey= {_this.updateLangKey} langKey={_this.state.langKey} updateIsLogin= {_this.updateIsLogin}></Header>
            <Left></Left>
            <Switch>   
                  {Routers.map((item, index) => {
                    return <Route key={index} path={item.path} exact render={props =>
                      (item.auth ? (<item.component {..._this.assignProps(props)} />) : (_this.state.isLogin ? <item.component {..._this.assignProps(props)} /> : <Redirect to={{
                        pathname: '/login',
                        state: { from: props.location }
                      }} />)
                      )} />
                  })}
            </Switch> 
          </React.Fragment>
        </Router>
         <Footer></Footer>
      </React.Fragment>
    );
  }
}